import json
from datetime import datetime

from django.core.paginator import Paginator
from django.http import JsonResponse
from django.views import View
from lapi_app.models.testcase_model.testcase import Testcase, Asserts, Parametric
from lapi_app.models.testcase_model.testcase_tree import Menu


class Singlecase(View):
    # 获取单个测试用例
    def post(self, request, *args, **kwargs):
        data = json.loads(request.body)
        case_id = data["menu_id"]  # 通过目录id帮绑定接口id
        menu = Menu.objects.get(id=case_id)
        c_id = menu.testcase_id
        asserts = Asserts.objects.filter(assert_testcase_id=c_id)
        assert_list = []

        for a in asserts:
            assert_dict = {
                "assert_key": a.assert_key,
                "assert_value": a.assert_value
            }
            assert_list.append(assert_dict)
        case = Testcase.objects.get(id=c_id)

        case_dict = {
            "menu_id": case_id,
            "id": menu.testcase_id,
            "name": case.t_name,
            "method": case.t_method,
            "api_type": case.api_type,
            "url": case.t_url,
            "body_type": case.t_type,
            "params": case.t_params,
            "headers": case.t_header,
            "extract": case.t_extract,
            "body": case.t_body,
            "environment_id": case.t_environment_id,
            "assert_form": assert_list,
            "detail": case.t_case_detail
        }
        return JsonResponse(case_dict)
